<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: jz
 * Date: 16/7/25
 * Time: 下午5:22
 */

class Article extends MY_Controller {
    private $error = '';
    
    
    
    public function index($page = '0', $per_page = 1)
    {
        
        $this->load->library('pagination');
        $conf['base_url'] = base_url('admin/article/index');
        $conf['total_rows'] = $this->db->count_all_results('article');
        $conf['per_page'] = $per_page;
        $conf['cur_page'] = $page;

        $this->pagination->initialize($conf);
        $links = $this->pagination->create_links();

        $this->db->limit($per_page, $page);
        $this->load->model('admin/article_model');
        $data = array(
            'article_list' => $this->article_model->get(),
            'links' => $links
        );
        $this->load->view('admin/article_list.html', $data);
    }
    
    
    
    
    public function issue_article()
    {
        $this->load->model('admin/cate_model');
        $this->load->model('admin/type_model');
        $cate = $this->cate_model->get();
        $type = $this->type_model->get();
        $data =array(
            'cate_table' => $cate,
            'type_table' => $type
        );
        $this->load->helper('form');
        $this->load->view('admin/article.html', $data);
    }

    private function get_upload_name()
    {
        $this->load->library('upload');
        $ret = $this->upload->do_upload('thumb');
        if (!$ret){
            $this->error = $this->upload->display_errors();
            return false;
        }
        $file = $this->upload->data();
        $my_conf['width'] = 200;
        $my_conf['height'] = 200;
        $my_conf['source_image'] = $file['full_path'];
        $my_conf['create_thumb'] = true;
        $my_conf['maintain_ratio'] = true;
        $my_conf['thumb_marker'] = '';
        $this->load->library('image_lib', $my_conf);
        if (!$this->image_lib->resize()){
            $this->error = $this->image_lib->display_errors();
            return false;
        }
        return $file['full_path'];
    }
    public function issue_article_doing()
    {
        $this->load->library('form_validation');
        $this->load->helper('form');
        $ret = $this->form_validation->run('article');
        if (!$ret){
            $this->issue_article();
            return;
        }
        $thumb = $this->get_upload_name();
        if ($thumb === false){
            echo $this->error;
            return;
        }
        
        $this->load->model('admin/article_model');
        $article = array(
            'title' => $this->input->post('title'),
            'type' => $this->input->post('type'),
            'cid' => $this->input->post('cid'),
            'info' => $this->input->post('info'),
            'thumb' => $thumb,
            'content' => $this->input->post('content'),
            'time' => time()
        );

        $ret = $this->article_model->issue($article);
        if (!$ret){
            echo '发表失败';
        }else {
            echo '发表成功';
        }
    }
    public function edit_article()
    {
        $this->load->helper('form');
        $this->load->view('admin/edit_article.html');
    }
    public function edit_article_doing()
    {
        $this->load->library('form_validation');
        $this->load->helper('form');
        $ret = $this->form_validation->run('article');
        if (!$ret){
            $this->load->view('admin/edit_article.html');
        }else {
            echo 'ok';
        }
    }
    
    public function del()
    {
        $aid = $this->input->post('aid'); 
        
        $this->load->model('admin/article_model');
        
        $result = $this->article_model->del($aid);
        if (!$result){
            $data = array(
                'code' => 'error',
                'msg' => '删除失败'
            );
            echo json_encode($data);
            return;
        }
        $data = array('code' => 'ok');
        echo json_encode($data);
        return;
        
    }
    
    
}